/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.gui.dao;

import br.gui.cliente.Hospede;
import br.gui.cliente.Hotel;
import br.gui.cliente.Reserva;
import br.gui.interfaces.IdAutoIncrement;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Anderson
 */
public class Container implements Serializable {

    private static Container container = null;
    private final static String nomeBanco = "banco.hilton";
    private List<Hotel> hoteis;
    private List<Hospede> hospedes;
    private List<Reserva> reservas;

    
    private Container() {
        hoteis = new ArrayList<Hotel>();
        hospedes = new ArrayList<Hospede>();
        reservas = new ArrayList<Reserva>();
    }

    	public int gerarId(String classe) {
		List lista = getList(classe);
		if(lista.size() == 0)
			return 0;
		
		Object obj = lista.get(lista.size() - 1);
		return ((IdAutoIncrement) obj).getId() + 1;
		
	}
    public static Container getInstance() {
        if (container == null) {
            container = new Container();
            openFile();
        }

        return container;

    }

    public synchronized static void openFile() {
        try {
            File banco = new File(nomeBanco);
            if (banco.exists()) {
                ObjectInputStream objReader = new ObjectInputStream(new FileInputStream(banco));
                container = (Container) objReader.readObject();
                objReader.close();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public synchronized static void saveFile() {
        try {
            File banco = new File(nomeBanco);
            ObjectOutputStream objWriter = new ObjectOutputStream(new FileOutputStream(banco));
            
            objWriter.writeObject(Container.getInstance());
            objWriter.flush();
            objWriter.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List getList(String classe) {
        if (classe.equals("br.gui.cliente.Hotel")) {
            return hoteis;
        }
        if (classe.equals("br.gui.cliente.Hospede")) {
            return hospedes;
        }
        if (classe.equals("br.gui.cliente.Reserva")) {
            return reservas;
        }
        return null;
    }

    public List<Hospede> getHospedes() {
        return hospedes;
    }

    public void setHospedes(List<Hospede> hospedes) {
        this.hospedes = hospedes;
    }

    public List<Hotel> getHoteis() {
        return hoteis;
    }

    public void setHoteis(List<Hotel> hoteis) {
        this.hoteis = hoteis;
    }

    public List<Reserva> getReservas() {
        return reservas;
    }

    public void setReservas(List<Reserva> reservas) {
        this.reservas = reservas;
    }
    
    
}
